package me.zhengjie.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.ppService.PPStationService;
import me.zhengjie.pojo.Resp;
import me.zhengjie.pojo.sqlServer.PPStation;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "出入库工作站管理")
@RequestMapping("/api/station")
public class PPStationController {

    private final PPStationService ppStationService;

    @Log("获取所有工作站信息")
    @ApiOperation("获取所有工作站信息")
    @GetMapping("/findAllStation")
    public Resp findAllStation(){
        try{
            List<PPStation> ppStations= ppStationService.getAllStation();
            return Resp.builder()
                    .data("ppStationList", ppStations)
                    .msg("获取工作站信息成功")
                    .status(200)
                    .build();
        }catch (Exception e){
            return Resp.builder()
                    .status(500)
                    .msg("获取工作站信息失败")
                    .build();
        }

    }

    @Log("获取所有出库工作站信息")
    @ApiOperation("获取所有出库工作站信息")
    @GetMapping("/findAllOutStation")
    public Resp findAllOutStation(){
        try{
            List<PPStation> ppStations= ppStationService.getAllStation();
            List<PPStation> ppOutStations =ppStations.stream().filter(station -> station.getStationType()==0).collect(Collectors.toList());
            return Resp.builder()
                    .data("ppOutStationsList", ppOutStations)
                    .msg("获取工作站信息成功")
                    .status(200)
                    .build();
        }catch (Exception e){
            return Resp.builder()
                    .status(500)
                    .msg("获取工作站信息失败")
                    .build();
        }

    }

    @Log("获取所有入库工作站信息")
    @ApiOperation("获取所有入库工作站信息")
    @GetMapping("/findAllInStation")
    public Resp findAllIStation(){
        try{
            List<PPStation> list= ppStationService.getAllStation();
            List<PPStation> ppInStations = list.stream().filter(station -> station.getStationType()==1).collect(Collectors.toList());
            return Resp.builder()
                    .data("ppInStationsList", ppInStations)
                    .msg("获取工作站信息成功")
                    .status(200)
                    .build();
        }catch (Exception e){
            return Resp.builder()
                    .status(500)
                    .msg("获取工作站信息失败")
                    .build();

        }

    }

    @Log("获取所有入库工作站信息")
    @ApiOperation("获取所有入库工作站信息")
    @GetMapping("/findStationStatus/{stationCode}")
    public Resp findStation(@PathVariable String stationCode){
        try{
            PPStation ppStation= ppStationService.getStationByCode(stationCode);
            return Resp.builder()
                    .data("ppStationStatus", ppStation.getStationStatus())
                    .msg("获取工作站状态成功")
                    .status(200)
                    .build();
        }catch (Exception e){
            return Resp.builder()
                    .status(500)
                    .msg("获取工作站状态失败")
                    .build();

        }

    }

    @Log("选择工作站")
    @ApiOperation("选择工作站")
    @PostMapping("/chooseStation/{stationCode}")
    public Resp chooseStation(@PathVariable String stationCode){
        /*if (ppStationService.startWorking(stationCode)){
            return Resp
                    .builder()
                    .status(200)
                    .msg("选择工作站成功")
                    .build();
        }else {
            return Resp
                    .builder()
                    .status(500)
                    .msg("选择工作站失败")
                    .build();
        }*/
        return Resp
                .builder()
                .status(200)
                .msg("选择工作站成功")
                .build();
    }

    @Log("关闭工作站")
    @ApiOperation("关闭工作站")
    @PostMapping("/closeStation/{stationCode}")
    public Resp closeStation(@PathVariable String stationCode){
        if (ppStationService.finishWorking(stationCode)){
            return Resp
                    .builder()
                    .status(200)
                    .msg("关闭工作站成功")
                    .build();
        }else {
            return Resp
                    .builder()
                    .status(500)
                    .msg("关闭工作站失败")
                    .build();
        }
    }




}
